import {defineStore} from "pinia";
import {immediatelyStoreType} from "@/store/modules/immediately/type";
import {getAllStsReport, getStsResultIntime} from "@/api/immediately";
import {stsReslutParamsType, stsReslutType} from "@/api/immediately/type";
import {timestampToDateTime24, timestampToMonthTime} from "@/utils/dateUtilsT";

let useImmediatelyStore = defineStore('useImmediately', {
    state(): immediatelyStoreType {
        return {
            time: [],
            reportId: 26,
            allReportOptions: [],
            table: [],
            headers: [],
            tableLoading: false,
            rightItem: {} as stsReslutType,
            menuRightList: [],
            defaultRightOpends: [],
            allTable: [],
            diguiOver: ''
        }
    },
    actions: {
        async useGetAllStsReport() {
            let res = await getAllStsReport()
            this.allReportOptions = res.data
        },
        async useGetStsResultIntime() {
            this.tableLoading = true
            // this.tableLoading = false
            // this.menuRightList = JSON.parse(window.sessionStorage.getItem('testList')!)
            // this.diguiOver = ''
            // this.findReportId(this.menuRightList, 'type', 2, 'item')
            // this.rightItem = this.diguiOver
            // this.setDefaultRightOpends()
            // this.allTable = []
            // this.findAllTable(this.menuRightList)
            try {
                let params: stsReslutParamsType = {
                    reportId: this.reportId,
                    stsEndTime: this.time.length > 1 ? this.time[1] : '',
                    stsStartTime: this.time.length > 0 ? this.time[0] : ''
                }
                let res = await getStsResultIntime(params)
                this.tableLoading = false
                this.menuRightList = this.setDigui(res.data)
                window.sessionStorage.setItem('testList', JSON.stringify(res.data))
                this.diguiOver = ''
                this.findReportId(this.menuRightList, 'type', 2, 'item')
                this.rightItem = this.diguiOver
                this.setDefaultRightOpends()
                this.allTable = []
                this.findAllTable(this.menuRightList)
            } catch (err) {
                this.tableLoading = false
            }

        },
        //@ts-ignore
        findReportId(list: any, key: string, value: string | number, returnName: string) {
            for (let i = 0; i < list.length; i++) {
                if (list[i][key] == value) {
                    if (this.diguiOver != '') {
                        return this.diguiOver
                    } else {
                        if (returnName == 'item') {
                            this.diguiOver = list[i]
                        } else {
                            this.diguiOver = list[i][returnName]
                        }
                        break;
                    }
                } else {
                    return this.findReportId(list[i].children, key, value, returnName)

                }
            }
        },
        setDigui(list: any[]) {
            for (let i = 0; i < list.length; i++) {
                list[i].id = list[i].id + ''
                if (list[i].children && list[i].children.length > 0) {
                    this.setDigui(list[i].children)
                }
            }
            return list
        },
        findAllTable(list: any[]) {
            for (let i = 0; i < list.length; i++) {
                if (list[i].type == 2) {
                    this.allTable.push(
                        list[i]
                    )
                } else {
                    if (list[i].children && list[i].children.length > 0) {
                        this.findAllTable(list[i].children)
                    }
                }
            }
        },
        setDefaultRightOpends() {
            this.defaultRightOpends = []
            this.menuRightList.forEach((item:stsReslutType) => {
                let id = item.id
                this.defaultRightOpends.push(id)
            })
        },
    }
})

export default useImmediatelyStore
